package com.myplaylist.android.persistence;

import java.util.ArrayList;
import java.util.List;

import com.myplaylist.android.model.Track;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class RepositoryManager implements IRepositoryManager {

	private static final int NO_OF_TRACKS = 10;
	private static RepositoryManager instance = null;
	private MyPlaylistSQLiteHelper dbHelper;
	private SQLiteDatabase database = null;
	private static final String[] ALL_COLUMNS = {
			MyPlaylistSQLiteHelper.COLUMN_AUTHOR,
			MyPlaylistSQLiteHelper.COLUMN_TITLE,
			MyPlaylistSQLiteHelper.COLUMN_NO_OF_PLAYBACKS };

	private RepositoryManager(Context context) {
		dbHelper = new MyPlaylistSQLiteHelper(context);
	}

	public static RepositoryManager getInstance(Context context) {
		if (instance == null) {
			instance = new RepositoryManager(context);
		}
		return instance;
	}

	public void add(Track track) {
		database = dbHelper.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(MyPlaylistSQLiteHelper.COLUMN_AUTHOR, track.getAuthor());
		values.put(MyPlaylistSQLiteHelper.COLUMN_TITLE, track.getTitle());
		

		Cursor cursor = database.rawQuery("select * from "
				+ MyPlaylistSQLiteHelper.TABLE_TRACKS + " where "
				+ MyPlaylistSQLiteHelper.COLUMN_AUTHOR + " = ? and "
				+ MyPlaylistSQLiteHelper.COLUMN_TITLE + " = ?", new String[] {
				track.getAuthor(), track.getTitle() });
		if (cursor.getCount() == 0) {
			values.put(MyPlaylistSQLiteHelper.COLUMN_NO_OF_PLAYBACKS, 1);
			database.insert(MyPlaylistSQLiteHelper.TABLE_TRACKS, null, values);
		} else {
			cursor.moveToFirst();
			values.put(MyPlaylistSQLiteHelper.COLUMN_NO_OF_PLAYBACKS, cursor.getInt(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_NO_OF_PLAYBACKS)) + 1);
			database.update(MyPlaylistSQLiteHelper.TABLE_TRACKS, values, ""
					+ MyPlaylistSQLiteHelper.COLUMN_AUTHOR + " = ? and "
					+ MyPlaylistSQLiteHelper.COLUMN_TITLE + " = ?",
					new String[] { track.getAuthor(), track.getTitle() });
		}

		cursor.close();
		
		dbHelper.close();
		database = null;
		
		printContents();
	}

	public List<Track> getTopTen() {
		List<Track> trackList = new ArrayList<Track>();
		
		database = dbHelper.getReadableDatabase();

		Cursor cursor = database.query(MyPlaylistSQLiteHelper.TABLE_TRACKS, ALL_COLUMNS, null, null, null, null, "" + MyPlaylistSQLiteHelper.COLUMN_NO_OF_PLAYBACKS + " desc");
		
		if (cursor.getCount() >= NO_OF_TRACKS) {
			cursor.moveToFirst();
			for (int i = 0; i < NO_OF_TRACKS; i++) {
				String author = cursor.getString(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_AUTHOR));
				String title = cursor.getString(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_TITLE));
				int noOfPlaybacks = cursor.getInt(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_NO_OF_PLAYBACKS));
				Log.d("NoOfPlayBacks", "" + noOfPlaybacks);
				cursor.moveToNext();
				
				Track tempTrack = new Track(title, author, NO_OF_TRACKS - i);
				Log.d("Track: ", tempTrack.toString());
				trackList.add(tempTrack);
			}
		}
		
		cursor.close();
		
		dbHelper.close();
		database = null;

		printContents();
		
		return trackList;
	}
	
	private void printContents() {
		database = dbHelper.getReadableDatabase();

		Cursor cursor = database.query(MyPlaylistSQLiteHelper.TABLE_TRACKS, ALL_COLUMNS, null, null, null, null, null);
		cursor.moveToFirst();
		
		for (int i = 0; i < cursor.getCount(); i++) {
			Log.i("No. " + i, "" + cursor.getString(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_TITLE)) + "|" + cursor.getString(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_AUTHOR)) + "|" + cursor.getInt(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_NO_OF_PLAYBACKS)));
			cursor.moveToNext();
		}
		
		/*int pos = 0;
		if (cursor.moveToFirst() && cursor.getCount() > 1) {
			while (! cursor.isLast()) {
				Log.i("" + (pos++), "" + cursor.getString(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_TITLE)) + "|" + cursor.getString(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_AUTHOR)) + "|" + cursor.getInt(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_NO_OF_PLAYBACKS)));
				cursor.moveToNext();
			}
		} else if (cursor.moveToFirst()) {
			Log.i("" + (pos++), "" + cursor.getString(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_TITLE)) + "|" + cursor.getString(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_AUTHOR)) + "|" + cursor.getInt(cursor.getColumnIndex(MyPlaylistSQLiteHelper.COLUMN_NO_OF_PLAYBACKS)));
		}*/
	
		cursor.close();
		dbHelper.close();
		database = null;
	}

}
